import com.svcon.jdbf.DBFWriter;
import com.svcon.jdbf.JDBFException;
import com.svcon.jdbf.JDBField;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

import categories.CustomerCategory;

public class OutputWriters {
    private final static Logger log = Logger.getLogger(OutputWriters.class);

    private static JDBField[] outFileFileds;

    static {
        try {
            outFileFileds = new JDBField[] {
                new JDBField("CUSNAME", 'C', 50, 0),
                new JDBField("BENCUSCODE", 'C', 10, 0),
                new JDBField("BENXXXCODE", 'C', 10, 0),
                new JDBField("BENSRVDATE", 'D', 8, 0),
                new JDBField("BENSRVTIME", 'C', 30, 0),
                new JDBField("BENDESC1", 'C', 20, 0),
                new JDBField("BENDESC2", 'C', 20, 0),
                new JDBField("BENCDRDUR", 'N', 9, 1),
                new JDBField("BENDURAT", 'N', 9, 1),
                new JDBField("BENSRV1ST", 'N', 13, 0),
                new JDBField("BENSRVRATE", 'N', 13, 0),
                new JDBField("BEN1STRATE", 'N', 13, 0),
                new JDBField("BENRATE", 'N', 13, 0),
                new JDBField("BENSRVTOT", 'N', 16, 0),
                new JDBField("BENCSTTOT", 'N', 16, 0)
            };
        } catch (JDBFException e) {
            e.printStackTrace();
        }
    }

    private final DBFWriter[] writers;

    public OutputWriters(
            File outDir, String outWhite, String outBlack, String outUncategorised
    ) throws IOException, JDBFException {

        String encoding = "cp1251";
        Locale.setDefault(Locale.ENGLISH);

        // sequence of writers in array is important!
        // later will be used for CustomerCategory-Writer matching
        this.writers = new DBFWriter[] {
                new DBFWriter(prepareNewOutFile(new File(outDir, outWhite)).getPath(), outFileFileds, encoding),
                new DBFWriter(prepareNewOutFile(new File(outDir, outBlack)).getPath(), outFileFileds, encoding),
                new DBFWriter(prepareNewOutFile(new File(outDir, outUncategorised)).getPath(), outFileFileds, encoding)
        };
    }

    public DBFWriter getFor(CustomerCategory category) {
        return writers[category.getIdx()];
    }

    private static File prepareNewOutFile(File outFile) throws IOException {
        if(outFile.exists()) {
            File archiveDir = new File(outFile.getParentFile(), "archive");
            FileUtils.forceMkdir(archiveDir);

            File toFile = File.createTempFile(
                    FilenameUtils.getBaseName(outFile.getName()) + ".",
                    "." + FilenameUtils.getExtension(outFile.getName()),
                    archiveDir
            );

            log.info("file " + outFile + " alredy exist, archiving it to " + toFile);

            FileUtils.copyFile(outFile, toFile);
            outFile.delete();
        }

        FileUtils.forceMkdir(outFile.getParentFile());
        outFile.createNewFile();

        return outFile;
    }

    public void closeAll() throws JDBFException {
        for (DBFWriter w : writers) {
            w.close();
        }
    }
}
